package com.jinkeen.xiaxie.customView

import android.content.Context
import android.content.res.Resources
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.jinkeen.xiaxie.R
import com.jinkeen.xiaxie.util.BitmapUtil
import java.util.*
import kotlin.collections.ArrayList


/**
 * @description: 表情下落
 * @author whz
 * @date 2022-08-03 17:08
 * @version 1.0
 * https://cloud.tencent.com/developer/article/1742572?from=article.detail.1726948
 */

class RainView : View {

    private var mPaint: Paint = Paint()

    //图片处理
    private var mMatrix: Matrix? = null
    private var random: Random = Random()

    private var isRun = false

    private var bitmapList: ArrayList<ItemEmoji>? = null
    var imgResId = R.mipmap.pay_ali

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        mPaint.isAntiAlias = true
        mPaint.isFilterBitmap = true
        mPaint.isDither = true
        mMatrix= Matrix()
        bitmapList= ArrayList()
    }
var url="https://www.baidu.com/link?url=QLD-Vva8WMz6JlGkWgPFw8zcKO5mlVeH7Qq5HuyhHWobUANTn-vXfUqW0WsIV60XoDisTxIGYBcrAiBY7H8dzQk6KcbGbQVIgorIDb6PTCRa6MLmLfEyliiF8mIbhk4zF7mn1_lWpnYAA45FimULLE6zat6T2eSjsR6bj1jG4G7Zo6bkEJWBuIbZgvVxLJnkHY7gvf-mjSp9Ja2hleRL4nla0zr5DRLKjdhZE0b_3EwWxux0LdJf4kzG_aPZSvR5qOHmRZr9bHwEARykOxcyDeJoixKkLzUCRnRpm_yQ88rlTIS9ZZwLs8Av7gTR3j084O24aS_Uir4d52zondnVwhdla7rdh_BIxjZSsLu1YoGl_GPgguPNYDxCiPtwQahs70R5UlPE22XYH5LCdyRs4mzWUkOMyFMLf5aDAEx0qC4kwAJr04EvaJgQYiauSaSI3AFzhUhpoyZu2MlBJ66b2m6xAyi6TMQsZr5ZnErY-_lGl_nIozcRf1ycsDo-hmMHXAjvxtUfjhcrpWY1B5UDzbLhTEL55gZPyaBB9IKBgU1kC8_rfyxJ1JhvOARlkqpqDTxD4MV3qL8JyY7GjW7OZ1tKGLI6C7dWJNw9Jh56GRIeQIttjZyO9a4KYHrKc0Zn&wd=&eqid=b6eb28d500030c460000000262eb4303"

    private fun initData() {
        for (i in 0 until 20) {
            val itemEmoji = ItemEmoji()
//            itemEmoji.bitmap= BitmapFactory.decodeResource(Resources.getSystem(), R.mipmap.pay_ali)
            itemEmoji.bitmap= BitmapUtil().base64ToBitmap("")

            //起始横坐标在[100,getWidth()-100) 之间
            itemEmoji.x = random.nextInt(width - 200) + 100
            //起始纵坐标在(-getHeight(),0] 之间，即一开始位于屏幕上方以外
            itemEmoji.y = -random.nextInt(height)
            //横向偏移[-2,2) ，即左右摇摆区间
            itemEmoji.offsetX = random.nextInt(4) - 2
            //纵向固定下落12
            itemEmoji.offsetY = 12
            //缩放比例[0.8,1.2) 之间
            itemEmoji.scale = ((random.nextInt(40) + 80) / 100f).toDouble()
            bitmapList?.add(itemEmoji)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (isRun) {
            //判断表情下落结束，结束不再重绘
            var isInScreen = false
            for (i in 0 until bitmapList?.size!!) {
                mMatrix?.reset()
                //缩放
                bitmapList?.get(i)?.scale?.toFloat()?.let {
                    mMatrix?.setScale(
                        it,
                        bitmapList!![i].scale.toFloat()
                    )
                }
                //下落过程坐标
                bitmapList!![i].x = bitmapList!![i].x + bitmapList!![i].offsetX
                bitmapList!![i].y = bitmapList!![i].y + bitmapList!![i].offsetY
                if (bitmapList!![i].y <= height) {
                    isInScreen = true
                }
                //位移
                mMatrix?.postTranslate(bitmapList!![i].x.toFloat(), bitmapList!![i].y.toFloat())
                bitmapList!![i].bitmap?.let { canvas?.drawBitmap(it, mMatrix!!, mPaint) }
            }
            if (isInScreen) {
                postInvalidate()
            } else {
                release()
            }
        }
    }

    private fun release() {
        if (bitmapList != null && bitmapList?.size != 0) {
            for (i in bitmapList!!.indices) {
                if (!bitmapList!![i].bitmap?.isRecycled!!) {
                    bitmapList!![i].bitmap?.recycle()
                }
            }
            bitmapList?.clear()
        }
    }

    fun start(isRun: Boolean) {
        this.isRun = isRun
        initData()
        postInvalidate()
    }

    class ItemEmoji {
        //左标
        var x: Int = 0
        var y: Int = 0

        //偏移
        var offsetX = 0
        var offsetY = 0

        //缩放
        var scale = 0.0

        //图片资源
        var bitmap: Bitmap?=null
    }
}